package com.creative.filter;

import com.creative.constant.ConstantPool;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.UUID;

/**
 * @author chaird
 * @create 2023-07-23 21:43
 */
@Activate(group = {"consumer", "provider"}, order = 0)
public class TraceFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        String trace = RpcContext.getContext().getAttachment(ConstantPool.TRACE);
        if (null == trace){
            RpcContext context = RpcContext.getContext();
            context.setAttachment(ConstantPool.TRACE, UUID.randomUUID().toString());
            trace = RpcContext.getContext().getAttachment(ConstantPool.TRACE);
        }


        System.out.println("trace:" + trace);

        return invoker.invoke(invocation);
    }
}
